#include "predict.h"
#include <string.h>

typedef void (*h264_predict_t)( pixel *src );

static void h264_predict_16x16_v(pixel *src)
{
    pixel *src_top = src - FDEC_STRIDE;

    for (int i = 0; i < 16; i++)
    {
        memcpy(src, src_top, 16 * sizeof(pixel));
        src += FDEC_STRIDE;
    }
}

static void h264_predict_16x16_h(pixel *src)
{
    
    for (int i = 0; i < 16; i++)
    {
        pixel *src_left = (src - 1);
        for (int j = 0; j < 16; j++) {
            src[j] = *src_left;
        }
        src += FDEC_STRIDE;
    }
}

#define PREDICT_16x16_DC(v)\
    for( int i = 0; i < 16; i++ )\
    {\
        MPIXEL_X4( src+ 0 ) = v;\
        MPIXEL_X4( src+ 4 ) = v;\
        MPIXEL_X4( src+ 8 ) = v;\
        MPIXEL_X4( src+12 ) = v;\
        src += FDEC_STRIDE;\
    }

static void predict_16x16_dc(pixel *src, pixel val)
{
    for (int i = 0; i < 16; i++)
    {
        for (int j = 0; j < 16; j++) {
            src[j] = val;
        }
        src += FDEC_STRIDE;
    }
}

static void h264_predict_16x16_dc( pixel *src )
{
    int dc = 0;

    for( int i = 0; i < 16; i++ )
    {
        dc += src[-1 + i * FDEC_STRIDE];        // 左边界像素和
        dc += src[i - FDEC_STRIDE];             // 上边界像素和
    }
    pixel val = (dc + 16) >> 5;                // 计算平均值

    // 填充16x16的像素块
    predict_16x16_dc(src, val);
}

static void h264_predict_16x16_p( pixel *src )
{
    int H = 0;
    int V = 0;

    /* calculate H and V */
    for( int i = 0; i <= 7; i++ )
    {
        H += ( i + 1 ) * ( src[ 8 + i - FDEC_STRIDE ] - src[6 -i -FDEC_STRIDE] );
        V += ( i + 1 ) * ( src[-1 + (8+i)*FDEC_STRIDE] - src[-1 + (6-i)*FDEC_STRIDE] );
    }

    int a = 16 * ( src[-1 + 15*FDEC_STRIDE] + src[15 - FDEC_STRIDE] );
    int b = ( 5 * H + 32 ) >> 6;
    int c = ( 5 * V + 32 ) >> 6;

    int i00 = a - b * 7 - c * 7 + 16;

    for( int y = 0; y < 16; y++ )
    {
        int pix = i00;
        for( int x = 0; x < 16; x++ )
        {
            src[x] = x264_clip_pixel( pix>>5 );
            pix += b;
        }
        src += FDEC_STRIDE;
        i00 += c;
    }
}

static void predict_16x16_dc_left( pixel *src )
{
    int dc = 0;

    for( int i = 0; i < 16; i++ ) {
        dc += src[-1 + i * FDEC_STRIDE];
    }
    predict_16x16_dc(src, (dc + 8) >> 4);
}

static void predict_16x16_dc_top( pixel *src )
{
    int dc = 0;
    for( int i = 0; i < 16; i++ ) {
        dc += src[i - FDEC_STRIDE];
    }
    predict_16x16_dc(src, (dc + 8) >> 4);
}
static void predict_16x16_dc_128( pixel *src )
{
    predict_16x16_dc(src, 0x80);
}

h264_predict_t predict_16x16_func[] = {
    [I_PRED_16x16_V] = h264_predict_16x16_v,
    [I_PRED_16x16_H] = h264_predict_16x16_h,
    [I_PRED_16x16_DC] = h264_predict_16x16_dc,
    [I_PRED_16x16_P] = h264_predict_16x16_p,
    [I_PRED_16x16_DC_LEFT] = predict_16x16_dc_left,
    [I_PRED_16x16_DC_TOP] = predict_16x16_dc_top,
    [I_PRED_16x16_DC_128] = predict_16x16_dc_128,
};

// 将I_PRED_16x16转换成实际编码中的预测类型
static const uint8_t x264_mb_pred_mode16x16_fix[I_PRED_16x16_DC_128 + 1] =
{
    I_PRED_16x16_V, I_PRED_16x16_H, I_PRED_16x16_DC, I_PRED_16x16_P,
    I_PRED_16x16_DC,I_PRED_16x16_DC,I_PRED_16x16_DC
};

intra16x16_pred_e h264_get_actual_pred16x16(intra16x16_pred_e mode)
{
    return x264_mb_pred_mode16x16_fix[mode];
}


void h264_predict_8x8c_v( pixel *src )
{
    pixel *src_top = src - FDEC_STRIDE;
    for( int i = 0; i < 8; i++ )
    {
        memcpy( src, src_top, 8 );
        src += FDEC_STRIDE;
    }
}

static void h264_predict_8x8c_h(pixel *src)
{
    for (int i = 0; i < 8; i++)
    {
        pixel *src_left = (src - 1);
        for (int j = 0; j < 8; j++) {
            src[j] = *src_left;
        }
        src += FDEC_STRIDE;
    }
}

static void h264_predict_8x8c_dc( pixel *src )
{
    int s0 = 0, s1 = 0, s2 = 0, s3 = 0;

    /*
          s0 s1
       s2
       s3
    */
    for( int i = 0; i < 4; i++ )
    {
        s0 += src[i - FDEC_STRIDE];
        s1 += src[i + 4 - FDEC_STRIDE];
        s2 += src[-1 + i * FDEC_STRIDE];
        s3 += src[-1 + (i+4)*FDEC_STRIDE];
    }
    int avg_val = (s0 + s1 + s2 + s3 + 8) >> 5;

    for (size_t i = 0; i < 8; i++)
    {
        for (size_t j = 0; j < 8; j++)
        {
            src[i + j] = avg_val;
        }
        src += FDEC_STRIDE;
    }
}

static void h264_predict_8x8c_dc_left( pixel *src )
{
    int avg_val = 0;

    for( int i = 0; i < 8; i++ )
    {
        avg_val += src[i * FDEC_STRIDE     - 1];
    }
    avg_val = (avg_val + 4) >> 4;

    for (size_t i = 0; i < 8; i++)
    {
        for (size_t j = 0; j < 8; j++)
        {
            src[i + j] = avg_val;
        }
        src += FDEC_STRIDE;
    }
}

static void predict_8x8_dc(pixel *src, pixel val)
{
    for (int i = 0; i < 8; i++)
    {
        for (int j = 0; j < 8; j++) {
            src[j] = val;
        }
        src += FDEC_STRIDE;
    }
}

static void h264_predict_8x8c_dc_top( pixel *src )
{
    int dc = 0;
    for( int i = 0; i < 8; i++ ) {
        dc += src[i - FDEC_STRIDE];
    }
    predict_8x8_dc(src, 0x80);
}

static void h264_predict_8x8c_dc_128( pixel *src )
{
    for (size_t i = 0; i < 8; i++)
    {
        for (size_t j = 0; j < 8; j++)
        {
            src[j] = 0x80;
        }
        src += FDEC_STRIDE;
    }
}

void h264_predict_8x8c_p( pixel *src )
{
    int H = 0, V = 0;

    for( int i = 0; i < 4; i++ )
    {
        H += ( i + 1 ) * ( src[4+i - FDEC_STRIDE] - src[2 - i -FDEC_STRIDE] );
        V += ( i + 1 ) * ( src[-1 +(i+4)*FDEC_STRIDE] - src[-1+(2-i)*FDEC_STRIDE] );
    }

    int a = 16 * ( src[-1+7*FDEC_STRIDE] + src[7 - FDEC_STRIDE] );
    int b = ( 17 * H + 16 ) >> 5;
    int c = ( 17 * V + 16 ) >> 5;
    int i00 = a -3*b -3*c + 16;

    for( int y = 0; y < 8; y++ )
    {
        int pix = i00;
        for( int x = 0; x < 8; x++ )
        {
            src[x] = x264_clip_pixel( pix>>5 );
            pix += b;
        }
        src += FDEC_STRIDE;
        i00 += c;
    }
}

h264_predict_t predict_chroma_func[] = {
    [I_PRED_CHROMA_DC] = h264_predict_8x8c_dc,
    [I_PRED_CHROMA_V] = h264_predict_8x8c_v,
    [I_PRED_CHROMA_H] = h264_predict_8x8c_h,
    [I_PRED_CHROMA_P] = h264_predict_8x8c_p,
    [I_PRED_CHROMA_DC_LEFT] = h264_predict_8x8c_dc_left,
    [I_PRED_CHROMA_DC_TOP] = h264_predict_8x8c_dc_top,
    [I_PRED_CHROMA_DC_128] = h264_predict_8x8c_dc_128,
};

void h264_predict_16x16(pixel *src, intra16x16_pred_e mode)
{
    predict_16x16_func[mode](src);
}

void h264_predict_chroma(pixel *src, intra_chroma_pred_e mode)
{
    predict_chroma_func[mode](src);
}